﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WheelClean2.Model;
using EntityFrameworkCore;
using WheelClean2.Views;

namespace WheelClean2.Mould
{
    public class AppDbContext : DbContext
    {
        public DbSet<Track> Tracks { get; set; }
        public DbSet<TraceLists> TraceLists { get; set; }

        public DbSet<Product> Products { get; set; }

        public DbSet<PLCInfos> PLCInfos { get; set; }

        public DbSet<LaserInfo> LaserInfos { get; set; }

        public DbSet<ProductBOM> ProductBOMs { get; set; }
        public DbSet<AlarmInfo> AlarmInfos { get; set; }
        public DbSet<CurrentAlarm> CurrentAlarms { get; set; }




        private static string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Environment.CurrentDirectory + "\\TrackList1.accdb" + ";";

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseJet(connectionString);
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            // 配置实体映射
            //modelBuilder.Entity<Track>()
            //    .HasIndex(p => p.ID)
            //    .IsUnique();
            //modelBuilder.Entity<Product>()
            //    .HasIndex(p => p.Name)
            //    .IsUnique();
            modelBuilder.Entity<PLCInfos>()
                .HasIndex(p => p.ID)
                .IsUnique();

            modelBuilder.Entity<AlarmInfo>().HasKey(t => t.AlarmID);
            modelBuilder.Entity<CurrentAlarm>().HasAlternateKey(t => t.AlarmID);









        }

        public void GetData()
        {
            using (var db = new AppDbContext())
            {
                // 查询所有产品
                var allProducts = db.Tracks.ToList();

                //// 条件查询
                //var expensiveProducts = await db.Products
                //    .Where(p => p.Price > 100)
                //    .OrderByDescending(p => p.Price)
                //    .ToListAsync();

                //// 单条查询
                //var product = await db.Products
                //    .FirstOrDefaultAsync(p => p.Name == "无线鼠标");

                //// 包含关联数据的查询
                //var customersWithOrders = await db.Customers
                //    .Include(c => c.Orders)  // 如果有订单关联
                //    .Where(c => c.Orders.Any())
                //    .ToListAsync();

                //// 分页查询
                //int pageNumber = 1;
                //int pageSize = 10;
                //var pagedProducts = await db.Products
                //    .OrderBy(p => p.Id)
                //    .Skip((pageNumber - 1) * pageSize)
                //    .Take(pageSize)
                //    .ToListAsync();
            }
        }
    }
}
